﻿/* ShortList类
 */

/* 未完事项
 * 1、单元测试。
 */
namespace LSLib.Collections
{
    using System;
    using System.Collections.Generic;

    public class ShortList<T> : IList<T>
    {
        #region 字段
        protected IList<T> m_Items;
        protected int m_MaxLength;
        private const int M_DefaultMaxLen = 1024;
        #endregion

        #region 属性
        public IList<T> Items
        {
            get { return m_Items; }
            private set { m_Items = value; }
        }

        public int MaxLength
        {
            get { return m_MaxLength; }
            set
            {
                if (value < M_DefaultMaxLen) value = M_DefaultMaxLen;
                m_MaxLength = value;
            }
        }
        #endregion

        #region 构造函数
        public ShortList() :
            this(M_DefaultMaxLen)
        { }

        public ShortList(int maxLength)
        {
            this.MaxLength = maxLength;
            this.Items = new List<T>();
        }

        public ShortList(IEnumerable<T> list) :
            this(M_DefaultMaxLen, list)
        { }

        public ShortList(int maxLength, IEnumerable<T> list)
        {
            this.MaxLength = maxLength;
            this.Items = new List<T>(list);
            this.checkMaxLength(this.Count);
        }
        #endregion

        #region 私有函数
        /// <summary>检查是否超出最大长度，若超出则抛出异常
        /// </summary>
        /// <param name="addLength">添加的长度</param>
        private void checkMaxLength(int addLength)
        {
            if (this.Items.Count + addLength > this.MaxLength)
                throw new IndexOutOfRangeException(string.Format("已经超出了最大长度：{0}。", this.MaxLength));
        }
        #endregion

        #region 实现IList接口
        public int IndexOf(T item)
        {
            return this.Items.IndexOf(item);
        }

        public void Insert(int index, T item)
        {
            this.checkMaxLength(1);
            this.Items.Insert(index, item);
        }

        public void RemoveAt(int index)
        {
            this.Items.RemoveAt(index);
        }

        public T this[int index]
        {
            get
            {
                return this.Items[index];
            }
            set
            {
                this.Items[index] = value;
            }
        }

        public void Add(T item)
        {
            this.checkMaxLength(1);
            this.Items.Add(item);
        }

        public void Clear()
        {
            this.Items.Clear();
        }

        public bool Contains(T item)
        {
            return this.Items.Contains(item);
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            this.Items.CopyTo(array, arrayIndex);
        }

        public int Count
        {
            get { return this.Items.Count; }
        }

        public bool IsReadOnly
        {
            get { return this.Items.IsReadOnly; }
        }

        public bool Remove(T item)
        {
            return this.Items.Remove(item);
        }

        public IEnumerator<T> GetEnumerator()
        {
            return this.Items.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }
        #endregion
    }
}
